<?php
namespace app\admin\controller;

/**
 * 授权操作
 */
class Author extends Base
{

    /**
     * 访问授权 menu
     */
    public function access()
    {
        $role_id = intval( input( 'role_id' ) );
        $type    = 'access';
        if( request()->isPost() ) {
            $data = input();
            if( isset( $data['rules'] ) ) {
                sort( $data['rules'] );
                $data['rules'] = implode( ',' , array_unique( $data['rules'] ) );
            }

            //更新
            $where       = [];
            $where['id'] = $role_id;
            $updateData          = [];
            $updateData['rules'] = isset( $data['rules'] ) && $data['rules'] ? $data['rules'] : '';

            $bool = db( 'auth_role' )->where( $where )->update( $updateData );
            if( $bool !== false ) {
                //操作日志
                add_system_log( $this->uid, 1, 2, '更新角色访问授权' );
                $this->success( '操作成功', url( 'AuthRole/index' ) );
            } else {
                $this->error( '操作失败' );
            }
        } else {
            $menu        = model( 'Menu' );
            $where       = [];
            $where['id'] = $role_id;
            $group_role  = db( 'auth_role' )->where( $where )->find();
            $node_list   = $menu->returnNodes();

            $this->assign( 'node_list', $node_list );
            $this->assign( 'type', $type );
            $this->assign( 'role_id', $role_id );
            $this->assign( 'group_role', $group_role );
            return $this->fetch();
        }
    }
    
    /**
     * 用户授权列表
     */
    public function member()
    {
        $role_id = input( 'role_id' );
        $type     = 'member';

        $where       = [];
        $where['id'] = $role_id;
        $group_role  = db( 'auth_role' )->where( $where )->find();

        $list = db( 'auth_role_member' )->where( ['role_id'=>$role_id] )->select();
        $data = [];
        $uidArr       = array_unique( array_column( $list, 'uid' ) );
		
        $where        = [];
        $where[] = ['uid','in', $uidArr];
		
        $field        = 'uid, username, nickname';
        $userRet      = db( 'auth_member' )->field( $field )->where( $where )->select();
		
        $userRet      = array_column( $userRet, null, 'uid' );
        foreach( $list as $k=>$v ) {
            $userInfo = isset( $userRet[$v['uid']] ) && $userRet[$v['uid']] ? 
                        $userRet[$v['uid']] : [];
            $data[$k]['uid']      = $v['uid'];
            $data[$k]['username'] = isset( $userInfo['username'] ) && $userInfo['username'] ? 
                                    $userInfo['username'] : '';
            $data[$k]['nickname'] = isset( $userInfo['nickname'] ) && $userInfo['nickname'] ? 
                                    $userInfo['nickname'] : '';
            $data[$k]['role_id']  = $v['role_id'];
        }
        $this->assign( 'type', $type );
        $this->assign( 'role_id', $role_id );
        $this->assign( 'group_role', $group_role );
        $this->assign( 'list', $data );
        return $this->fetch( 'access' );
    }
    
    /**
     * 取消授权
     */
    public function cleanbind()
    {
        $param = input();
        if( $param['uid'] && $param['role_id'] ) {
            $where = [
                'uid'     => $param['uid'],
                'role_id' => $param['role_id'],
            ];
            $bool = db( 'auth_role_member' )->where( $where )->delete();
            if( $bool ) {
                //操作日志
                add_system_log( $this->uid, 1, 2, '撤销角色访问授权' );
                $this->success( '撤销成功' );
            } else {
                $this->error( '撤销失败' );
            }
        } else {
            $this->error( '撤销失败' );
        }
    }
    
}